#!/usr/bin/python3
# coding=utf-8

import json
import sys
import os
import time
###################################################################
## 如前端输入
## {
##	"instance":"192.168.1.101",
##	"threshold":"10000",
##	"timeout":"10",
## }
## '{"instance":"192.168.1.101", "timeout":20, "threshold":50}'
## 解析参数方法:
## sysak schedmoni -j -s 20 50
##  50 门限, 捕获延迟超过50ms的事件
##  -s 20 诊断时间20秒
##
######################################################################
class Param(dict):
    def __missing__(self,key):
        sys.stderr.write("The input parameter check failed, there have no parameter '%s'"%key)
        exit(1)

args = Param(json.loads(sys.argv[1]))

result = {}
result['commands'] = []

cmd0 = {}
cmd0['instance'] = args["instance"]
arg_time = args.get("timeout", "20")
if arg_time:
	arg_time = '-s ' + str(arg_time)
arg_thresh = args.get("threshold", "20")

dump_log_cmd = "cat /var/log/sysak/schedmoni/schedmoni.json;"
schedmoni_cmd = "sysak -g schedmoni -j "+str(arg_time)+" "+str(arg_thresh)+" > /dev/null"
cmd0['cmd'] = schedmoni_cmd+" && "+dump_log_cmd

result['commands'].append(cmd0)

data = json.dumps(result)
print(data)
